namespace MeetingAssistant.Component.Data.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class LogTable : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "Companies",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        CompanyName = c.String(nullable: false, maxLength: 20, storeType: "nvarchar"),
                        CompanyAddress = c.String(nullable: false, maxLength: 200, storeType: "nvarchar"),
                        Code = c.String(nullable: false, maxLength: 18, storeType: "nvarchar"),
                        Enabled = c.Boolean(nullable: false),
                        UpdateDate = c.DateTime(nullable: false, precision: 0),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "Users",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        UserName = c.String(nullable: false, maxLength: 20, storeType: "nvarchar"),
                        Password = c.String(nullable: false, maxLength: 32, storeType: "nvarchar"),
                        TrueName = c.String(nullable: false, maxLength: 20, storeType: "nvarchar"),
                        Sex = c.Boolean(nullable: false),
                        IdentityNumber = c.String(nullable: false, maxLength: 18, storeType: "nvarchar"),
                        Email = c.String(nullable: false, maxLength: 50, storeType: "nvarchar"),
                        Phone = c.String(maxLength: 13, storeType: "nvarchar"),
                        Address = c.String(maxLength: 150, storeType: "nvarchar"),
                        Department = c.String(nullable: false, maxLength: 20, storeType: "nvarchar"),
                        Enabled = c.Boolean(nullable: false),
                        UpdateDate = c.DateTime(nullable: false, precision: 0),
                        Company_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("Companies", t => t.Company_Id)
                .Index(t => t.Company_Id);
            
            CreateTable(
                "Roles",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        RoleName = c.String(nullable: false, maxLength: 20, storeType: "nvarchar"),
                        Description = c.String(maxLength: 100, storeType: "nvarchar"),
                        Enabled = c.Boolean(nullable: false),
                        OrderSort = c.Int(nullable: false),
                        UpdateDate = c.DateTime(nullable: false, precision: 0),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "Permissions",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false, maxLength: 20, storeType: "nvarchar"),
                        Code = c.String(unicode: false),
                        Description = c.String(maxLength: 100, storeType: "nvarchar"),
                        Enabled = c.Boolean(nullable: false),
                        ModuleId = c.Int(nullable: false),
                        UpdateDate = c.DateTime(nullable: false, precision: 0),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("Modules", t => t.ModuleId)
                .Index(t => t.ModuleId);
            
            CreateTable(
                "Modules",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ParentId = c.Int(),
                        Name = c.String(nullable: false, maxLength: 20, storeType: "nvarchar"),
                        LinkUrl = c.String(nullable: false, maxLength: 50, storeType: "nvarchar"),
                        IsMenu = c.Boolean(nullable: false),
                        Code = c.Int(nullable: false),
                        Description = c.String(maxLength: 100, storeType: "nvarchar"),
                        Enabled = c.Boolean(nullable: false),
                        UpdateDate = c.DateTime(nullable: false, precision: 0),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("Modules", t => t.ParentId)
                .Index(t => t.ParentId);
            
            CreateTable(
                "UserGroups",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        GroupName = c.String(nullable: false, maxLength: 20, storeType: "nvarchar"),
                        Description = c.String(maxLength: 100, storeType: "nvarchar"),
                        OrderSort = c.Int(nullable: false),
                        Enabled = c.Boolean(nullable: false),
                        UpdateDate = c.DateTime(nullable: false, precision: 0),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "PermissionRoles",
                c => new
                    {
                        Permission_Id = c.Int(nullable: false),
                        Role_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Permission_Id, t.Role_Id })
                .ForeignKey("Permissions", t => t.Permission_Id, cascadeDelete: true)
                .ForeignKey("Roles", t => t.Role_Id, cascadeDelete: true)
                .Index(t => t.Permission_Id)
                .Index(t => t.Role_Id);
            
            CreateTable(
                "UserGroupRoles",
                c => new
                    {
                        UserGroup_Id = c.Int(nullable: false),
                        Role_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.UserGroup_Id, t.Role_Id })
                .ForeignKey("UserGroups", t => t.UserGroup_Id, cascadeDelete: true)
                .ForeignKey("Roles", t => t.Role_Id, cascadeDelete: true)
                .Index(t => t.UserGroup_Id)
                .Index(t => t.Role_Id);
            
            CreateTable(
                "UserGroupUsers",
                c => new
                    {
                        UserGroup_Id = c.Int(nullable: false),
                        User_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.UserGroup_Id, t.User_Id })
                .ForeignKey("UserGroups", t => t.UserGroup_Id, cascadeDelete: true)
                .ForeignKey("Users", t => t.User_Id, cascadeDelete: true)
                .Index(t => t.UserGroup_Id)
                .Index(t => t.User_Id);
            
            CreateTable(
                "RoleUsers",
                c => new
                    {
                        Role_Id = c.Int(nullable: false),
                        User_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Role_Id, t.User_Id })
                .ForeignKey("Roles", t => t.Role_Id, cascadeDelete: true)
                .ForeignKey("Users", t => t.User_Id, cascadeDelete: true)
                .Index(t => t.Role_Id)
                .Index(t => t.User_Id);
            
        }
        
        public override void Down()
        {
            DropForeignKey("RoleUsers", "User_Id", "Users");
            DropForeignKey("RoleUsers", "Role_Id", "Roles");
            DropForeignKey("UserGroupUsers", "User_Id", "Users");
            DropForeignKey("UserGroupUsers", "UserGroup_Id", "UserGroups");
            DropForeignKey("UserGroupRoles", "Role_Id", "Roles");
            DropForeignKey("UserGroupRoles", "UserGroup_Id", "UserGroups");
            DropForeignKey("PermissionRoles", "Role_Id", "Roles");
            DropForeignKey("PermissionRoles", "Permission_Id", "Permissions");
            DropForeignKey("Permissions", "ModuleId", "Modules");
            DropForeignKey("Modules", "ParentId", "Modules");
            DropForeignKey("Users", "Company_Id", "Companies");
            DropIndex("RoleUsers", new[] { "User_Id" });
            DropIndex("RoleUsers", new[] { "Role_Id" });
            DropIndex("UserGroupUsers", new[] { "User_Id" });
            DropIndex("UserGroupUsers", new[] { "UserGroup_Id" });
            DropIndex("UserGroupRoles", new[] { "Role_Id" });
            DropIndex("UserGroupRoles", new[] { "UserGroup_Id" });
            DropIndex("PermissionRoles", new[] { "Role_Id" });
            DropIndex("PermissionRoles", new[] { "Permission_Id" });
            DropIndex("Modules", new[] { "ParentId" });
            DropIndex("Permissions", new[] { "ModuleId" });
            DropIndex("Users", new[] { "Company_Id" });
            DropTable("RoleUsers");
            DropTable("UserGroupUsers");
            DropTable("UserGroupRoles");
            DropTable("PermissionRoles");
            DropTable("UserGroups");
            DropTable("Modules");
            DropTable("Permissions");
            DropTable("Roles");
            DropTable("Users");
            DropTable("Companies");
        }
    }
}
